<?php
/**
 * slideshow_flash -- Supports showing slideshows of images in an album using a custom flash-based player.
 *
 *  Plugin Option 'slideshow_flash_fadetime' -- Time for the cross-fade from one image to the next
 *  Plugin Option 'slideshow_flash_slidetime' -- Time to show each slide for (this can be changed
 *    when the slide show is displayed).
 *  Plugin Option 'slideshow_flash_sourcesize' -- Source size for the images to use for the
 *    slideshow. Not much point making this bigger than your source images!
 *  Plugin Option 'slideshow_flash_showcaptions' -- Choose whether to show (true) or hide (false)
 *    the captions by default when navigating to a slideshow.
 *
 * The theme file 'slideshow_flash.php' must reside in the theme folder. If you are creating a custom
 * theme, copy these files form the "default" theme of the Zenphoto distribution.
 *
 * @author Mark Hughes
 * @version 1.0.1.0
 * @package plugins
 */

$plugin_description = gettext("Adds a theme function to call a Flash-based slideshow with high-quality full screen mode. Additionally the file <em>slideshow_flash.php</em> must exist in the theme for this plugin to work.");
$plugin_author = "Mark Hughes";
$plugin_version = '1.1.0.0';
$plugin_URL = "http://www.deltawebdev.co.uk/projects/zenphotoSlideshowFlash/";
$option_interface = new slideshowFlashOptions();

/**
 * Plugin option handling class
 *
 */
class slideshowFlashOptions {

  function slideshowFlashOptions() {
    setOptionDefault('slideshow_flash_fadetime', '1.0');
    setOptionDefault('slideshow_flash_slidetime', '5');
    setOptionDefault('slideshow_flash_sourcesize', '1024');
    setOptionDefault('slideshow_flash_showcaptiosn', 'false');
  }


  function getOptionsSupported() {
    return array( gettext('Fade time') => array('key' => 'slideshow_flash_fadetime', 'type' => 0,
                    'desc' => gettext("Time for the cross-fade between slides. Fractions of a second are allowed (e.g. 0.5, 1.25).")),
                  gettext('Slide time') => array('key' => 'slideshow_flash_slidetime', 'type' => 0,
                    'desc' => gettext("Time for each slide to be displayed (default) in seconds. Note that this can be changed in the interface when a slideshow is playing. MUST be one of the following values: 1, 3, 5, 7, 10, 20, 30.")),
                  gettext('Slide source resolution') => array('key' => 'slideshow_flash_sourcesize', 'type' => 0,
                    'desc' => gettext("The source size for the images which will power the slideshow. Note that they will always be displayed full screen/full browser, so setting this too small will only reduce the quality, not the size. Likewise, there is no point setting this larger than the size of your source images.")),
                  gettext('Show captions (default)') => array('key' => 'slideshow_flash_showcaptions', 'type' => 0,
                    'desc' => gettext("'true' to show captions, 'false' to hide them. Note that this can still be controlled from the user interface, this changes whether captions are displayed when first accessing a slideshow."))
    );
  }

  function handleOption($option, $currentValue) {
  }

}

$slideshow_flash_instance = 0;

/**
 * Prints a link to call the slideshow (not shown if there are no images in the album)
 * To be used on album.php and image.php
 * A CSS id names 'slideshowlink' is attached to the link so it can be directly styled.
 *
 * @param string $linktext Text for the link
 */
function printSlideShowFlashLink($linktext='') {
  global $_zp_current_image, $_zp_current_album, $_zp_current_search, $slideshow_flash_instance;
  if (checkForPassword(true)) return;
  if(empty($_GET['page'])) {
    $pagenr = 1;
  } else {
    $pagenr = $_GET['page'];
  }
  $slideshowhidden = '';
  if (in_context(ZP_SEARCH)) {
    $imagenumber = '';
    $imagefile = '';
    $albumnr = 0;
    $slideshowlink = rewrite_path("/page/slideshow_flash","index.php?p=slideshow_flash");
    $slideshowhidden = '<input type="hidden" name="preserve_search_params" value="'.$_zp_current_search->getSearchParams().'" />';
  } else {
    if(in_context(ZP_IMAGE)) {
      $imagenumber = imageNumber();
      $imagefile = $_zp_current_image->filename;
    } else {
      $imagenumber = "";
      $imagefile = "";
    }
    if (in_context(ZP_SEARCH_LINKED)) {
      $albumnr = -getAlbumID();
    } else {
      $albumnr = getAlbumID();
    }
	if ( $imagenumber != "" )
	{
		$slideshowlink = rewrite_path(pathurlencode($_zp_current_album->getFolder())."/page/slideshow_flash?slideStartInd=".$imagenumber, "index.php?p=slideshow_flash&amp;slideStartInd=".$imagenumber."&amp;album=".urlencode($_zp_current_album->getFolder()));
	}
	else
	{
		$slideshowlink = rewrite_path(pathurlencode($_zp_current_album->getFolder())."/page/slideshow_flash", "index.php?p=slideshow_flash&amp;album=".urlencode($_zp_current_album->getFolder()));
	}
  }
  $numberofimages = getNumImages();
  if($numberofimages != 0) {
    ?>
                <a id="slideshowlink_<?php echo $slideshow_flash_instance; ?>" href="<?php echo $slideshowlink; ?>"><?php echo $linktext; ?></a>
<?php }
  $slideshow_flash_instance ++;
}


/**
 * Prints the slideshow using the high-quality flash slideshow. It will start from the first image in the album regardless
 * of where it is called from.
 * To be used on slideshow_flash.php only and called from album.php or image.php.
 * Uses the full-size images for the album.
 *
 * NOTE: This does not support movie and audio files. If you need to show them please use the Flash mode of the normal slideshow plugin.
 */
function printSlideShowFlash() {
  $albumid = getAlbumID();
  $slideStartInd = 0;
  if ( $_GET["slideStartInd"] != null )
  {
	$slideStartInd = $_GET["slideStartInd"] - 1;
  }
  $count = 0;
  $gallery = new Gallery();
  $albumq = query_single_row("SELECT title, folder FROM ". prefix('albums') ." WHERE id = ".$albumid);
  $album = new Album($gallery, $albumq['folder']);
  $albumtitle = $album->getTitle();
  if(!checkAlbumPassword($albumq['folder'], $hint)) {
    echo gettext("This album is password protected!"); exit;
  }
  $dynamic = $album->isDynamic();
  $images = $album->getImages(0);

  // slideshow display section

  // Get configuration options
  $sourceSize = getOption("slideshow_flash_sourcesize");
  $slideTime = getOption("slideshow_flash_slidetime");
  $slideTransitionTime = getOption("slideshow_flash_fadetime");
  $showCaptionsDefault = getOption("slideshow_flash_showcaptions");

  // Create the image and description lists.
  $count = 0;
  $imageList = "";
  $descriptionList = "";
        foreach($images as $animage) {
          if ($dynamic) {
                  $folder = $animage['folder'];
                        $filename = $animage['filename'];
                        $image = newImage($dalbum, $filename);
                        if ( $image->getWidth() > $image->getHeight() )
      {
        $imagepath = WEBPATH.'/'.ZENFOLDER.'/i.php?a='.pathurlencode($folder).'&i='.urlencode($filename).'&w='.$sourceSize;
      } else {
        $imagepath = WEBPATH.'/'.ZENFOLDER.'/i.php?a='.pathurlencode($folder).'&i='.urlencode($filename).'&h='.$sourceSize;
      }
                } else {
                        $folder = $album->name;
                        $filename = $animage;
                        $image = newImage($album, $filename);
                        if ( $image->getWidth() > $image->getHeight() )
      {
        $imagepath = WEBPATH.'/'.ZENFOLDER.'/i.php?a='.pathurlencode($folder).'&i='.urlencode($filename).'&w='.$sourceSize;
      } else {
        $imagepath = WEBPATH.'/'.ZENFOLDER.'/i.php?a='.pathurlencode($folder).'&i='.urlencode($filename).'&h='.$sourceSize;
      }
                }
    if ($count!=0)
    {
      $imageList .= "||";
      $descriptionList .= "||";
    }
    $imageList .= urlencode($imagepath);
    $descriptionList .= str_replace("%00","",urlencode(html_entity_decode($image->getDesc())));

                $count++;
        }
  ?>

  <script type="text/javascript">
    var flashvars = {
      imageList: "<?php print $imageList; ?>",
      descriptionList: "<?php print $descriptionList; ?>",
      slideTime: "<?php print $slideTime; ?>",
      slideTransitionTime: "<?php print $slideTransitionTime; ?>",
      showCaptionsDefault: "<?php print $showCaptionsDefault; ?>",
	  slideStartInd: "<?php print $slideStartInd; ?>"
    };
    var params = {
      menu: "false",
      scale: "noScale",
      bgcolor: "#000000",
      allowFullScreen: "true"
    };
    swfobject.embedSWF(
      "<?php echo WEBPATH . '/' . ZENFOLDER ?>/plugins/slideshow_flash/FullScreenSlideshow.swf",
      "slideShowFlash", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params);
  </script>
<?php
}

function printSlideShowFlashJS() {
?>

        <script src="<?php echo FULLWEBPATH . '/' . ZENFOLDER ?>/plugins/slideshow_flash/js/swfobject.js" type="text/javascript"></script>
<?php
}


?>
